package com.yuyou.tas.lesson.service.impl;


import com.yuyou.tas.lesson.mapper.LessonMaterialMapper;
import com.yuyou.tas.lesson.service.IBaseMaterialService;
import com.yuyou.tas.lesson.service.ILessonMaterialService;
import com.yuyou.tas.lesson.service.ILessonService;
import com.yuyou.tas.service.api.lesson.dto.DtoLessonMaterial;
import com.yuyou.tas.service.api.lesson.dto.DtoLessonMaterialWithName;
import com.yuyou.tas.service.api.lesson.entity.BaseMaterial;
import com.yuyou.tas.service.api.lesson.entity.Lesson;
import com.yuyou.tas.service.api.lesson.entity.LessonMaterial;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.Pagination;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.enums.CommEnums;
import com.yuyou.tas.util.exception.BusinessException;
import com.yuyou.tas.util.tool.DefindBeanUtils;
import com.yuyou.tas.util.tool.Sequence;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class LessonMaterialServiceImpl implements ILessonMaterialService {

    @Autowired
    private LessonMaterialMapper mapper;

    @Autowired
    private IBaseMaterialService materialService;

    @Autowired
    private ILessonService lessonService;

    @Override
    public int insert(LessonMaterial record){
    	record.setId(Sequence.getSequence().nextId());
    	record.setCreateTime(new Date());
    	return mapper.insert(record);
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public ResBoolSimpleInfo addLessonMaterial(Long lessonId, String[] materialIds,Integer isStan, Long pId, LoginRedisObj user, Long organId) throws Exception {
        Lesson l = lessonService.selectByPrimaryKey(lessonId);
        if(l == null || l.getBaseSectionId() == null ||  l.getBaseSectionId().equals("")){
            throw new BusinessException("课程信息异常");
        }
        for (int i = 0 ; i < materialIds.length ; i++){
            BaseMaterial bm = materialService.selectByPrimaryKey(Long.valueOf(materialIds[i]));
            if(bm != null){
                /*if(!bm.getBaseSectionId().equals(l.getBaseSectionId())){
                    throw new BusinessException("该素材年级与课程年级不同");
                }*/
                if(!bm.getDataSubject().equals(l.getDataSubject())){
                    throw new BusinessException("该素材科目与课程科目不符");
                }
                LessonMaterial lm = new LessonMaterial();
                lm.setId(Sequence.getSequence().nextId());
                lm.setCreateTime(new Date());
                lm.setCreateUser(user.getUserId());
                lm.setAuthOrganizationId(organId);
                lm.setpId(pId);
                lm.setLessonId(lessonId);
                lm.setSource(CommEnums.materialSource.m_library.getValue());
                lm.setIsStan(isStan);
                lm.setBaseMaterialId(bm.getId());
                mapper.insert(lm);
            }
        }
        ResBoolSimpleInfo info = new ResBoolSimpleInfo(true,"插入成功",null);
        return info;
    }

    @Override
    @Transactional(rollbackFor=Exception.class,timeout=60)
    public ResBoolSimpleInfo delLessonMaterial(Long id) {
        ResBoolSimpleInfo info = new ResBoolSimpleInfo(true,"删除成功",null);
        mapper.deleteByPrimaryKey(id);
        return info;
    }

    @Override
    public int findLessonMaterialCount(Pagination<DtoLessonMaterial> queryPgn) {
        return mapper.findLessonMaterialCount(queryPgn);
    }

    @Override
    public List<DtoLessonMaterial> findLessonMaterialPageList(Pagination<DtoLessonMaterial> queryPgn) {
        return mapper.findLessonMaterialPageList(queryPgn);
    }

    @Override
    public LessonMaterial selectByPKandOrgan(Long id, Long authOrganizationId) {
        return mapper.selectByPKandOrgan(id, authOrganizationId);
    }

	@Override
	public List<LessonMaterial> findLeMatByLessonId(Long lessonId) {
		return mapper.findLeMatByLessonId(lessonId);
	}

	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo<String> copylessonMaterial(Long pLessonId,Lesson currRecord) {
		//课程素材
		List<LessonMaterial> lmList=this.findLeMatByLessonId(pLessonId);
		for(LessonMaterial lm:lmList){
			LessonMaterial newLm=new LessonMaterial();
			DefindBeanUtils.copyProperties(lm, newLm);	
			newLm.setLessonId(currRecord.getId());
			newLm.setIsStan(currRecord.getIsStan());
			newLm.setpId(currRecord.getIsStan()==1?null:pLessonId);
			newLm.setAuthOrganizationId(currRecord.getAuthOrganizationId());
			newLm.setCreateUser(currRecord.getCreateUser());
			this.insert(newLm);			
		}
		return ResBoolSimpleInfo.getSuccessInfo();
	}

    @Override
    public LessonMaterial findByLessonMaterialId(Long lessonId, Long m_id) {
        return mapper.findByLessonMaterialId(lessonId , m_id);
    }

    @Override
    public List<DtoLessonMaterialWithName> findLeMatNameByLessonId(Long lessonId) {
        return mapper.findLeMatNameByLessonId(lessonId);
    }

    @Override
    public List<String> findBaseMatInLesson(Long baseMaterialId) {
        return mapper.findBaseMatInLesson(baseMaterialId);
    }

    @Override
    public Integer findCountBaseMatInLesson(Long baseMaterialId) {
        return mapper.findCountBaseMatInLesson(baseMaterialId);
    }

    @Override
    public void delByless(Long lessonId) {
        mapper.delByless(lessonId);
    }
}